package com.yoso.shop.interfaces.controller.commodity;

import com.alibaba.fastjson.JSON;
import com.yoso.base.interfaces.controller.BaseController;
import com.yoso.base.interfaces.controller.BaseResponse;
import com.yoso.base.service.IBaseService;
import com.yoso.shop.application.service.ICommodityBranchService;
import com.yoso.shop.domain.commodity.CommodityBranchEntity;
import com.yoso.shop.domain.commodity.CommodityBranchEntityExample;
import com.yoso.shop.infrastructure.cache.CommodityCacheServer;
import com.yoso.shop.interfaces.vo.commodity.CommodityBranchVo;
import com.yoso.shop.interfaces.vo.commodity.CommodityClassifySelectVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author yoso
 * @date 2021/12/8
 */
@RestController
@RequestMapping("/commodityClassify")
@Slf4j
@CrossOrigin
@Api(tags = "商品分类")
public class CommodityClassifyController extends BaseController<CommodityBranchVo, CommodityBranchEntity> {
    @Autowired
    private ICommodityBranchService commodityBranchService;
    @Autowired
    private CommodityCacheServer commodityCacheServer;

    @Override
    public CommodityBranchEntity V2E(CommodityBranchVo data) {
        return JSON.parseObject(JSON.toJSONString(data), CommodityBranchEntity.class);
    }

    @Override
    public CommodityBranchVo E2V(CommodityBranchEntity data) {
        return JSON.parseObject(JSON.toJSONString(data), CommodityBranchVo.class);

    }

    @Override
    public IBaseService<CommodityBranchVo, CommodityBranchEntityExample> getService() {
        return this.commodityBranchService;
    }

    @ApiOperation("获得商品主分支")
    @GetMapping("getMasterBranch")
    public BaseResponse getMasterBranch() {
        return this.success("操作成功", commodityCacheServer.getMasterVoList());
    }

    @ApiOperation("获得商品分支信息")
    @GetMapping("getCommodityBranchInfo")
    public BaseResponse<Map<String, Object>> getMasterBranchToBranchMap() {
        Map<String, Object> commodityBranchCacheMap = commodityCacheServer.getCommodityBranchCacheMap();
        return this.success("操作成功！", commodityBranchCacheMap);
    }

    @ApiOperation("获得商品分类级联下拉列表")
    @GetMapping("getCommodityClassifySelectInfo")
    public BaseResponse getCommodityClassifySelectInfo() {
        List<CommodityClassifySelectVo> selectVoList = commodityCacheServer.getSelectVoList();
        return this.success("操作成功！", selectVoList);
    }

    @Override
    public BaseResponse saveOrUpdate(CommodityBranchVo vo) {
        BaseResponse response = super.saveOrUpdate(vo);
        if (response.isSuccess()) {
            this.commodityCacheServer.clearCommodityBranchCache();
            this.commodityCacheServer.init();
        }
        return response;
    }
}
